package com.solcoo.service;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.stereotype.Service;

import com.solcoo.dao.DataDao;
import com.solcoo.json.DownloadToExcelByYongHuJson;
import com.solcoo.json.ShanghuDataJson;
import com.solcoo.json.ShanghuSimpleJson;
import com.solcoo.util.ReportPicUtil;

@Service
public class DataService {

	@Resource
	private DataDao dataDao;
	
	/**
	 * 获取年实收款总数
	 */
	public Double getTotalSskByYear(String year, Long yonghum) throws Exception {
		return dataDao.getTotalSskByYear(year, yonghum);
	}
	
	/**
	 * 获取月实收款总数数组
	 */
	public Double[] getSsks(String year, String picPath, Long yonghum) throws Exception {
		Calendar cal = Calendar.getInstance();
		
		int month = cal.get(Calendar.MONTH) + 1;
		
		Double[] ssks = new Double[12];
		
		String yearAndMonth = null;
		for (int i = 0; i < 12; i ++) {
			if (i < 9)
				yearAndMonth = year + "-0" + (i+1);
			else
				yearAndMonth = year + "-" + (i+1);
			
			if ((i+1) > month)
				ssks[i] = null;
			else
				ssks[i] = dataDao.getSskByYearAndMonth(yearAndMonth, yonghum);			
		}
		
		// 生成实收款折线图
		DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
		
		String xLabel = null;
		for (int i = 0; i < ssks.length; i++) {
			if (i < 9) 
				xLabel = "0" + (i+1) + "月";
			else
				xLabel = (i+1) + "月";
			
			if ((i+1) > month)
				linedataset.addValue(null, "月实收款", xLabel);
			else
				linedataset.addValue(ssks[i], "月实收款", xLabel);
		}
		
		JFreeChart chartLineChart = ChartFactory.createLineChart(
				"Solcoo " + year + " 年实收款 (折线图)", // 图表标题
				"月份", // 目录轴的显示标签
				"实收金额(元)", // 数值轴的显示标签
			     linedataset, // 数据
			     PlotOrientation.VERTICAL, // 水平显示图像
			     true, // include legend
			     true, // 是否生成工具
			     false // 是否生成URL链接
		);
		
		ReportPicUtil.createLineCharts(chartLineChart, picPath, "实收款折线图.jpg");
		
		return ssks;
	}
	
	/**
	 * 获取年会员总数
	 */
	public Integer getTotalVipCountByYear(String year, Long yonghum) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
		
		param.put("year", year);
		param.put("yonghum", yonghum);
		
		return dataDao.getTotalVipCountByYear(param);
	}
	
	/**
	 * 获取年用户总数
	 */
	public Integer getTotalYongHuCountByYear(String year) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
		
		param.put("year", year);
		
		return dataDao.getTotalYongHuCountByYear(param);
	}
	
	/**
	 *  获取年商户总数
	 */
	public Integer getTotalShangHuCountByYear(String year) throws Exception {
		return dataDao.getTotalShangHuCountByYear(year);
	}
	
	/**
	 * 获取截止到单月的会员总数数组
	 */
	public Integer[] getVipCount(String year, String picPath, Long yonghum) throws Exception {
		Calendar cal = Calendar.getInstance();
		
		int month = cal.get(Calendar.MONTH) + 1;
		
		Integer[] vipCounts = new Integer[12];
		
		String yearAndMonth = null;
		for (int i = 0; i < 12; i ++) {
			if (i < 9)
				yearAndMonth = year + "-0" + (i+1);
			else
				yearAndMonth = year + "-" + (i+1);
			
			if ((i+1) > month)
				vipCounts[i] = null;
			else
				vipCounts[i] = dataDao.getVipCountByYearAndMonth(yearAndMonth, yonghum);
		}
	
		// 生成实收款折线图
		DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
		
		String xLabel = null;
		for (int i = 0; i < vipCounts.length; i++) {
			if (i < 9)
				xLabel = "0" + (i+1) + "月";
			else
				xLabel = (i+1) + "月";
			
			if ((i+1) > month)
				linedataset.addValue(null, "月会员数", xLabel);
			else
				linedataset.addValue(vipCounts[i], "月会员数", xLabel);
		}
		
		JFreeChart chartLineChart = ChartFactory.createLineChart(
				"Solcoo " + year + " 年会员数 (折线图)", // 图表标题
				"月份", // 目录轴的显示标签
				"会员数(个)", // 数值轴的显示标签
			     linedataset, // 数据
			     PlotOrientation.VERTICAL, // 水平显示图像
			     true, // include legend
			     true, // 是否生成工具
			     false // 是否生成URL链接
		);
		
		ReportPicUtil.createLineCharts(chartLineChart, picPath, "会员数折线图.jpg");
		
		return vipCounts;
	}
	
	/**
	 * 获取截止到单月的用户总数数组
	 */
	public Integer[] getYongHuCount(String year, String picPath) throws Exception {
		Calendar cal = Calendar.getInstance();
		
		int month = cal.get(Calendar.MONTH) + 1;
		
		Integer[] vipCounts = new Integer[12];
		
		String yearAndMonth = null;
		for (int i = 0; i < 12; i ++) {
			if (i < 9)
				yearAndMonth = year + "-0" + (i+1);
			else
				yearAndMonth = year + "-" + (i+1);
			
			if ((i+1) > month)
				vipCounts[i] = null;
			else
				vipCounts[i] = dataDao.getYongHuCountByYearAndMonth(yearAndMonth);
		}
	
		// 生成实收款折线图
		DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
		
		String xLabel = null;
		for (int i = 0; i < vipCounts.length; i++) {
			if (i < 9)
				xLabel = "0" + (i+1) + "月";
			else
				xLabel = (i+1) + "月";
			
			if ((i+1) > month)
				linedataset.addValue(null, "月用户数", xLabel);
			else
				linedataset.addValue(vipCounts[i], "月用户数", xLabel);
		}
		
		JFreeChart chartLineChart = ChartFactory.createLineChart(
				"Solcoo " + year + " 年用户数 (折线图)", // 图表标题
				"月份", // 目录轴的显示标签
				"用户数(个)", // 数值轴的显示标签
			     linedataset, // 数据
			     PlotOrientation.VERTICAL, // 水平显示图像
			     true, // include legend
			     true, // 是否生成工具
			     false // 是否生成URL链接
		);
		
		ReportPicUtil.createLineCharts(chartLineChart, picPath, "用户数折线图.jpg");
		
		return vipCounts;
	}
	
	/**
	 * 获取月交易笔数数组
	 */
	public Integer[] getBusinessCountByYearAndMonth(String year, String picPath, Long yonghum) throws Exception {
		Calendar cal = Calendar.getInstance();
		
		int month = cal.get(Calendar.MONTH) + 1;
		
		Integer[] businessCounts = new Integer[12];
		
		String yearAndMonth = null;
		for (int i = 0; i < 12; i ++) {
			if (i < 9)
				yearAndMonth = year + "-0" + (i+1);
			else
				yearAndMonth = year + "-" + (i+1);
			
			if ((i+1) > month)
				businessCounts[i] = null;
			else
				businessCounts[i] = dataDao.getBusinessCountByYearAndMonth(yearAndMonth, yonghum);
		}
	
		// 生成交易笔数折线图
		DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
		
		String xLabel = null;
		for (int i = 0; i < businessCounts.length; i++) {
			if (i < 9)
				xLabel = "0" + (i+1) + "月";
			else
				xLabel = (i+1) + "月";
			
			if ((i+1) > month)
				linedataset.addValue(null, "交易笔数", xLabel);
			else
				linedataset.addValue(businessCounts[i], "交易笔数", xLabel);
		}
		
		JFreeChart chartLineChart = ChartFactory.createLineChart(
				"Solcoo " + year + " 年交易笔数 (折线图)", // 图表标题
				"月份", // 目录轴的显示标签
				"交易笔数(笔)", // 数值轴的显示标签
			     linedataset, // 数据
			     PlotOrientation.VERTICAL, // 水平显示图像
			     true, // include legend
			     true, // 是否生成工具
			     false // 是否生成URL链接
		);
		
		ReportPicUtil.createLineCharts(chartLineChart, picPath, "交易笔数折线图.jpg");
		
		return businessCounts;
	}
	
	/**
	 * 获取月发短信条数
	 */
	public Integer[] getMessageCountByYearAndMonth(String year, String picPath, Long yonghum) throws Exception {
		Calendar cal = Calendar.getInstance();
		
		int month = cal.get(Calendar.MONTH) + 1;
		
		Integer[] messageCounts = new Integer[12];
		
		String yearAndMonth = null;
		for (int i = 0; i < 12; i ++) {
			if (i < 9)
				yearAndMonth = year + "-0" + (i+1);
			else
				yearAndMonth = year + "-" + (i+1);
			
			if ((i+1) > month)
				messageCounts[i] = null;
			else
				messageCounts[i] = dataDao.getZhuYonghuJYCount(yearAndMonth, yonghum)*2 + dataDao.getZhiYonghuJYCount(yearAndMonth, yonghum)*3 + dataDao.getZhuYonghuCXCount(yearAndMonth, yonghum)*2 + dataDao.getZhiYonghuCXCount(yearAndMonth, yonghum)*3 + dataDao.getShouciGZCount(yearAndMonth, yonghum)*0;
		}
	
		// 生成商户数量折线图
		DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
		
		String xLabel = null;
		for (int i = 0; i < messageCounts.length; i++) {
			if (i < 9)
				xLabel = "0" + (i+1) + "月";
			else
				xLabel = (i+1) + "月";
			
			if ((i+1) > month)
				linedataset.addValue(null, "发短信条数", xLabel);
			else
				linedataset.addValue(messageCounts[i], "发短信条数", xLabel);
		}
		
		JFreeChart chartLineChart = ChartFactory.createLineChart(
				"Solcoo " + year + " 年发短信条数 (折线图)", // 图表标题
				"月份", // 目录轴的显示标签
				"发短信条数(条)", // 数值轴的显示标签
			     linedataset, // 数据
			     PlotOrientation.VERTICAL, // 水平显示图像
			     true, // include legend
			     true, // 是否生成工具
			     false // 是否生成URL链接
		);
		
		ReportPicUtil.createLineCharts(chartLineChart, picPath, "发短信条数折线图.jpg");
		
		return messageCounts;
	}

	/**
	 * 获取商户数量数组
	 */
	public Integer[] getMerchantCountByYearAndMonth(String year, String picPath) throws Exception {
		Calendar cal = Calendar.getInstance();
		
		int month = cal.get(Calendar.MONTH) + 1;
		
		Integer[] merchantCounts = new Integer[12];
		
		String yearAndMonth = null;
		for (int i = 0; i < 12; i ++) {
			if (i < 9)
				yearAndMonth = year + "-0" + (i+1);
			else
				yearAndMonth = year + "-" + (i+1);
			
			if ((i+1) > month)
				merchantCounts[i] = null;
			else
				merchantCounts[i] = dataDao.getMerchantCountByYearAndMonth(yearAndMonth);
		}
	
		// 生成商户数量折线图
		DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
		
		String xLabel = null;
		for (int i = 0; i < merchantCounts.length; i++) {
			if (i < 9)
				xLabel = "0" + (i+1) + "月";
			else
				xLabel = (i+1) + "月";
			
			if ((i+1) > month)
				linedataset.addValue(null, "商户数量", xLabel);
			else
				linedataset.addValue(merchantCounts[i], "商户数量", xLabel);
		}
		
		JFreeChart chartLineChart = ChartFactory.createLineChart(
				"Solcoo " + year + " 年商户数量 (折线图)", // 图表标题
				"月份", // 目录轴的显示标签
				"商户数量(个)", // 数值轴的显示标签
			     linedataset, // 数据
			     PlotOrientation.VERTICAL, // 水平显示图像
			     true, // include legend
			     true, // 是否生成工具
			     false // 是否生成URL链接
		);
		
		ReportPicUtil.createLineCharts(chartLineChart, picPath, "商户数量折线图.jpg");
		
		return merchantCounts;
	}
	
	/**
	 * 获取商户数据
	 */
	public List<ShanghuDataJson> getShanghuData() throws Exception {
		return dataDao.getShanghuData();
	}
	
	/**
	 * 获取简单的商户信息
	 */
	public List<ShanghuSimpleJson> getShanghuSimple() throws Exception {
		return dataDao.getShanghuSimple();
	}
	
	/**
	 * 获取商户名称
	 */
	public String getShanghumcByYonghum(Long yonghum) throws Exception {
		return dataDao.getShanghumcByYonghum(yonghum);
	}

	/**
	 * 获取商户月新增会员数量数组
	 */
	public Integer[] getAddVipsByShanghu(String year, String picPath, Long yonghum) throws Exception {
		Calendar cal = Calendar.getInstance();
		
		int month = cal.get(Calendar.MONTH) + 1;
		
		Integer[] addVips = new Integer[12];
		
		String yearAndMonth = null;
		for (int i = 0; i < 12; i ++) {
			if (i < 9)
				yearAndMonth = year + "-0" + (i+1);
			else
				yearAndMonth = year + "-" + (i+1);
			
			if ((i+1) > month)
				addVips[i] = null;
			else
				addVips[i] = dataDao.getAddVipsByShanghu(yearAndMonth, yonghum);
		}
		
		// 生成商户月新增会员数量折线图
		DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
		
		String xLabel = null;
		for (int i = 0; i < addVips.length; i++) {
			if (i < 9)
				xLabel = "0" + (i+1) + "月";
			else
				xLabel = (i+1) + "月";
			
			if ((i+1) > month)
				linedataset.addValue(null, "月新增会员数量", xLabel);
			else
				linedataset.addValue(addVips[i], "月新增会员数量", xLabel);
		}
		
		JFreeChart chartLineChart = ChartFactory.createLineChart(
				"Solcoo " + year + " 年商户月新增会员数量 (折线图)", // 图表标题
				"月份", // 目录轴的显示标签
				"月新增会员数量(个)", // 数值轴的显示标签
			     linedataset, // 数据
			     PlotOrientation.VERTICAL, // 水平显示图像
			     true, // include legend
			     true, // 是否生成工具
			     false // 是否生成URL链接
		);
		
		ReportPicUtil.createLineCharts(chartLineChart, picPath, "商户月新增会员数量折线图.jpg");
		
		return addVips;
	}

	// 生成近 30 天每日新增用户数量折线图
	public void createYongHuCountByDay30(String picPath) throws Exception {
		Calendar cal = Calendar.getInstance();
		
		cal.add(Calendar.DAY_OF_MONTH, -29);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdfmd = new SimpleDateFormat("MM-dd");
		
		Integer[] yongHuCounts = new Integer[30];
		String[] mds = new String[30];
		
		for (int i = 0; i < 30; i++) {
			yongHuCounts[i] = dataDao.getYongHuCountsByDay(sdf.format(cal.getTime()));
			mds[i] = sdfmd.format(cal.getTime());
			
			cal.add(Calendar.DAY_OF_MONTH, 1);
		}
		
		DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
		
		for (int i = 0; i < mds.length; i++) {
			linedataset.addValue(yongHuCounts[i], "日新增用户数量", mds[i]);
		}
		
		JFreeChart chartLineChart = ChartFactory.createLineChart(
				"Solcoo 近 30 天日用户数量 (折线图)", // 图表标题
				"日期", // 目录轴的显示标签
				"日新增用户数量(个)", // 数值轴的显示标签
			     linedataset, // 数据
			     PlotOrientation.VERTICAL, // 水平显示图像
			     true, // include legend
			     true, // 是否生成工具
			     false // 是否生成URL链接
		);
		
		ReportPicUtil.createLineCharts30(chartLineChart, picPath, "近30天日新增用户数量折线图.jpg");
	}

	// 生成近 30 天日实收款折线图
	public void createSsksByDay30(String picPath, Long yonghum) throws Exception {
		Calendar cal = Calendar.getInstance();
		
		cal.add(Calendar.DAY_OF_MONTH, -29);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdfmd = new SimpleDateFormat("MM-dd");
		
		Double[] ssks = new Double[30];
		String[] mds = new String[30];
		
		for (int i = 0; i < 30; i++) {
			ssks[i] = dataDao.getSsksByDay(sdf.format(cal.getTime()), yonghum);
			mds[i] = sdfmd.format(cal.getTime());
			
			cal.add(Calendar.DAY_OF_MONTH, 1);
		}
		
		DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
		
		for (int i = 0; i < mds.length; i++) {
			linedataset.addValue(ssks[i], "日实收款", mds[i]);
		}
		
		JFreeChart chartLineChart = ChartFactory.createLineChart(
				"Solcoo 近 30 天日实收款 (折线图)", // 图表标题
				"日期", // 目录轴的显示标签
				"日实收款(元)", // 数值轴的显示标签
			     linedataset, // 数据
			     PlotOrientation.VERTICAL, // 水平显示图像
			     true, // include legend
			     true, // 是否生成工具
			     false // 是否生成URL链接
		);
		
		if (yonghum == null) {
			ReportPicUtil.createLineCharts30(chartLineChart, picPath, "近30天日实收款折线图.jpg");
		} else {
			ReportPicUtil.createLineCharts30ByShangHu(chartLineChart, picPath, "近30天日实收款折线图.jpg");
		}
	}
	
	// 生成近 30 天每日新增会员数量折线图
	public void createVIPCountByDay30(String picPath, Integer yonghum) throws Exception {
		Calendar cal = Calendar.getInstance();
		
		cal.add(Calendar.DAY_OF_MONTH, -29);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdfmd = new SimpleDateFormat("MM-dd");
		
		Integer[] vipCounts = new Integer[30];
		String[] mds = new String[30];
		
		for (int i = 0; i < 30; i++) {
			vipCounts[i] = dataDao.getVIPCountsByDay(sdf.format(cal.getTime()), yonghum);
			mds[i] = sdfmd.format(cal.getTime());
			
			cal.add(Calendar.DAY_OF_MONTH, 1);
		}
		
		DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
		
		for (int i = 0; i < mds.length; i++) {
			linedataset.addValue(vipCounts[i], "日新增会员数量", mds[i]);
		}
		
		JFreeChart chartLineChart = ChartFactory.createLineChart(
				"Solcoo 近 30 天日会员数量 (折线图)", // 图表标题
				"日期", // 目录轴的显示标签
				"日新增会员数量(个)", // 数值轴的显示标签
			     linedataset, // 数据
			     PlotOrientation.VERTICAL, // 水平显示图像
			     true, // include legend
			     true, // 是否生成工具
			     false // 是否生成URL链接
		);
		
		ReportPicUtil.createLineCharts30ByShangHu(chartLineChart, picPath, "近30天日新增会员数量折线图.jpg");
	}
	
	/**
	 * 获取用户使用情况
	 */
	public List<DownloadToExcelByYongHuJson> getYongHuSYQK() throws SQLException {
		return dataDao.getYongHuSYQK();
	}
	
}